package org.windblog.core.entity;

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.windblog.core.PostService;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Comment {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String author;
	
	@Persistent
	private String authorEmail;
	
	@Persistent
	private String authorURL;
	
	@Persistent
	private String authorIP;
	
	@Persistent
	private Date date;
	
	@Persistent
	private String content;
	
	@Persistent
	private String agent;
	
	@Persistent
	private String type;
	
	@Persistent
	private int parent;
	
	@Persistent
	private Key postKey;
	
	public Key getPostKey() {
		return postKey;
	}

	public void setPostKey(Key post) {
		this.postKey = post;
	}

	public Post getPost()
	{
		return PostService.getPost(postKey);
	}
	
	public Comment()
	{
		unapprove();
		this.setDate(new Date());
	}
	
	public Key getKey() {
		return key;
	}

	public String getStrKey()
	{
		return KeyFactory.keyToString(key);
	}
	
	public void setKey(Key key) {
		this.key = key;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthorEmail() {
		return authorEmail;
	}

	public void setAuthorEmail(String authorEmail) {
		this.authorEmail = authorEmail;
	}

	public String getAuthorURL() {
		return authorURL;
	}

	public void setAuthorURL(String authorURL) {
		this.authorURL = authorURL;
	}

	public String getAuthorIP() {
		return authorIP;
	}

	public void setAuthorIP(String authorIP) {
		this.authorIP = authorIP;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getAgent() {
		return agent;
	}

	public void setAgent(String agent) {
		this.agent = agent;
	}

	public String getType() {
		return type;
	}
 
	public void setType(String type) {
		this.type = type;
	}

	public int getParent() {
		return parent;
	}

	public void setParent(int parent) {
		this.parent = parent;
	}
	
	public void unapprove()
	{
		this.setType("PENDING");
	}
	
	public void approve()
	{
		this.setType("APPROVED");
	}
	
	public void spam()
	{
		this.setType("SPAM");
	}
	
	public boolean isApproved()
	{
		if(this.getType().equals("APPROVED"))
			return true;
		else
			return false;
	}
	
	public boolean isSpam()
	{
		if(this.getType().equals("SPAM"))
			return true;
		else
			return false;
	}
}
